import json

import requests
from django.conf import settings


class Chack_msg():
    @classmethod
    def get_access_token(self):
        appid = 'wx4448a0c7c9d306e8'
        secret = '14a3cd8963ad34df86a1f0917ef0c667'
        url = 'https://api.weixin.qq.com/cgi-bin/token' + "?appid=" + appid + '&secret=' + secret + '&grant_type=client_credential'
        res = requests.get(url)
        return res.json()['access_token']

    @classmethod
    def msg_sec_check(self, openid, content):
        access_token = self.get_access_token()
        data = {
            "openid": openid,
            "scene": 2,
            "version": 2,
            "content": content
        }
        headers = {"Content-Type": "application/json;charset=utf-8"}
        url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=' + access_token
        res = requests.post(url, json=data, headers=headers)
        print(res.json())
        # print(res.json()['result']['label'] == 100)
        return True if res.json()['result']['label'] == 100 else False

# msg_sec_check('oVdG51IhqLPjMdYUK3YD3fBBa1A0', '卖枪')
